package com.asktgapp.user.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.asktgapp.R;
import com.asktgapp.application.ApiUtil;
import com.asktgapp.application.Apiservice;
import com.asktgapp.application.MyApplication;
import com.asktgapp.architecture.retrofit.ApiException;
import com.asktgapp.architecture.retrofit.ApiResponseBody;
import com.asktgapp.architecture.rxjava.ExceptionHandle;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.eventbus.PayFinishEvent;
import com.asktgapp.hxsg.BiuEaseHelper;
import com.asktgapp.model.HxAccountVO;
import com.asktgapp.modulebase.common.util.PreferencesUtil;
import com.asktgapp.modulebase.common.util.Util;
import com.asktgapp.user.activity.PaySuccessActivity;
import com.asktgapp.wxapi.WXPayEntryActivity;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMCmdMessageBody;
import com.hyphenate.chat.EMMessage;

import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * time:2018/1/26
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description:
 */

public class PaySuccessFragment extends BaseFragment {
    private TextView mStarChat, tip;
    private HxAccountVO bean;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_pay_success, container, false);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
//        getBaseActivity().setBackNavigationIcon();
        mStarChat = rootView.findViewById(R.id.tv_back);
        tip = rootView.findViewById(R.id.tip);
        mStarChat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到咨询界面
//                BiuEaseHelper.getInstance().beginSingleChat(getActivity(), bean.getProjector_hx(), "", "");
                if (MyApplication.mAskType > 2) {
                    PayFinishEvent.postEvent(true);
                   getBaseActivity().onBackPressed();
                } else {
                    BiuEaseHelper
                            .getInstance()
                            .beginSingleChat(getActivity(), "", bean.getProjector_hx(), MyApplication.mAskEngineerName
                                    , MyApplication.mAskEngineerHeadIMG, MyApplication.mAskEngineerID
                                    , 0, bean.getCid() + "", MyApplication.mAskType);
                    {
                        EMMessage cmdMsg = EMMessage.createSendMessage(EMMessage.Type.CMD);
                        String action = "开始";//action可以自定义
                        EMCmdMessageBody cmdBody = new EMCmdMessageBody(action);

                        String toUsername = bean.getProjector_hx();//发送给某个人
                        cmdMsg.setTo(toUsername);
                        cmdMsg.addBody(cmdBody);
                        EMClient.getInstance().chatManager().sendMessage(cmdMsg);
                    }
                    getActivity().finish();
                }
            }
        });

    }

    @Override
    public void loadData() {

        if (MyApplication.mAskType > 2) {
            tip.setText("向 " + MyApplication.mAskEngineerName + " 付款成功");
            mStarChat.setText("确定");
        } else {
            visibleLoading();
            Apiservice apiservice = ApiUtil.getInstance().create();
            HashMap params = new HashMap();

            if (Util.isEmpty(PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID)) || Util.isEmpty(MyApplication.mAskEngineerID) || Util.isEmpty(MyApplication.mAskType + "")) {
                showTost("缺少参数", 1);
                return;
            }
            params.put("userid", PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID));
            params.put("chat_userid", MyApplication.mAskEngineerID);
            params.put("type", MyApplication.mAskType);
            params.put("is_type", 1);
            Call<ApiResponseBody<HxAccountVO>> call = apiservice.chatToUser(params);
            call.enqueue(new Callback<ApiResponseBody<HxAccountVO>>() {
                @Override
                public void onResponse(Call<ApiResponseBody<HxAccountVO>> call, Response<ApiResponseBody<HxAccountVO>> response) {
                    inVisibleLoading();
                    if (response.isSuccessful()) {
                        bean = response.body().getResult();
                    } else {
                        showTost(response.raw().message(), 1);
                    }
                }

                @Override
                public void onFailure(Call<ApiResponseBody<HxAccountVO>> call, Throwable t) {
                    inVisibleLoading();
                    ApiException apiException = ExceptionHandle.handleException(t);
                    if (apiException.isTokenInvalid()) {
                        showUnLoginSnackbar();
                    } else if (apiException.isNetConnected()) {
                        showSetNetworkSnackbar();
                    } else {
                        showTost(apiException.getMessage(), 0);
                    }
                }
            });
        }
    }
}
